{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ones = np.array([1,0,0,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 1, 0]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1 if elem == 0 else 0 for elem in ones]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_set = np.unique(np.array([1,1,0,0,1,0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True], dtype=bool)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(np.array([1,1,0,0,1,0]).ravel()) == np.array([0,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "valid_elems = [0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "elemts = np.unique(np.array([1,1.0,0,0.0,1,0]).ravel())\n",
    "\n",
    "elems_are_valid = [True if elem in valid_elems else False for elem in elemts]\n",
    "\n",
    "np.array(elems_are_valid).all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True], dtype=bool)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(np.array([0,0,0,0,0,0])) == np.array([0]).all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# original code\n",
    "def ranking_precision_score(y_true, y_score, k=None):\n",
    "    \"\"\"Precision at rank k\n",
    "    Parameters\n",
    "    ----------\n",
    "    y_true : array-like, shape = [n_samples]\n",
    "        Ground truth (true relevance labels).\n",
    "    y_score : array-like, shape = [n_samples]\n",
    "        Predicted scores.\n",
    "    k : int\n",
    "        Rank.\n",
    "    Returns\n",
    "    -------\n",
    "    precision @k : float\n",
    "    \"\"\"\n",
    "    unique_y = np.unique(y_true)\n",
    "\n",
    "    if len(unique_y) > 2:\n",
    "        raise ValueError(\"Only supported for two relevance levels.\")\n",
    "\n",
    "    # edited by felipe:\n",
    "    # just assume the true value is 1 and false is zero\n",
    "    # otherwise we get errors when, for instance, the label array is [1,1,1,1]\n",
    "    # pos_label = unique_y[1]\n",
    "\n",
    "    pos_label = 1\n",
    "    n_pos = np.sum(y_true == pos_label)\n",
    "\n",
    "    order = np.argsort(y_score)[::-1]\n",
    "    y_true = np.take(y_true, order[:k])\n",
    "    n_relevant = np.sum(y_true == pos_label)\n",
    "\n",
    "    # Divide by min(n_pos, k) such that the best achievable score is always 1.0.\n",
    "#     return float(n_relevant) / min(n_pos, k)\n",
    "    return float(n_relevant) / k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "0.5\n"
     ]
    }
   ],
   "source": [
    "scores = np.array([\n",
    "    [0.5,0.4,0.1],\n",
    "    [0.0,0.8,0.2]\n",
    "])\n",
    "\n",
    "actual = np.array([\n",
    "    [1,0,0],\n",
    "    [1,0,1]\n",
    "])\n",
    "\n",
    "print(ranking_precision_score(actual[0], scores[0], k=2))\n",
    "print(ranking_precision_score(actual[1], scores[1], k=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n"
     ]
    }
   ],
   "source": [
    "scores = np.array([\n",
    "    [0.1, 0.4, 0.35, 0.8]\n",
    "])\n",
    "\n",
    "actual = np.array([\n",
    "    [0,0,1,1]\n",
    "])\n",
    "\n",
    "print(ranking_precision_score(actual[0], scores[0], k=4))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Global TF Kernel (Python 3)",
   "language": "python",
   "name": "global-tf-python-3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
